﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Web;
using System.Security.Cryptography;


namespace DNA.Mvc.Publishing
{
    public class CommentWrapper : Comment
    {
        private Comment innerComment;

        public CommentWrapper(Comment comment)
        {
            innerComment = comment;
        }

        #region Override

        public override Article Article
        {
            get
            {
                return innerComment.Article;
            }
            set
            {
                innerComment.Article = value;
            }
        }

        public override int ArticleID
        {
            get
            {
                return innerComment.ArticleID;
            }
            set
            {
                innerComment.ArticleID = value;
            }
        }

        public override string Body
        {
            get
            {
                return innerComment.Body;
            }
            set
            {
                innerComment.Body = value;
            }
        }

        public override string Email
        {
            get
            {
                return innerComment.Email;
            }
            set
            {
                innerComment.Email = value;
            }
        }

        public override int ID
        {
            get
            {
                return innerComment.ID;
            }
            set
            {
                innerComment.ID = value;
            }
        }

        public override string WebSite
        {
            get
            {
                return innerComment.WebSite;
            }
            set
            {
                innerComment.WebSite = value;
            }
        }

        public override string UserName
        {
            get
            {
                return innerComment.UserName;
            }
            set
            {
                innerComment.UserName = value;
            }
        }

        public override DateTime Posted
        {
            get
            {
                return innerComment.Posted;
            }
            set
            {
                if (innerComment != null)
                    innerComment.Posted = value;
            }
        }

        public override string Title
        {
            get
            {
                return innerComment.Title;
            }
            set
            {
                innerComment.Title = value;
            }
        }

        public override bool IsTrackback
        {
            get
            {
                return innerComment.IsTrackback;
            }
            set
            {
                innerComment.IsTrackback = value;
            }
        }

        public override bool IsPingback
        {
            get
            {
                return innerComment.IsPingback;
            }
            set
            {
                innerComment.IsPingback = value;
            }
        }

        public override bool IsApproved
        {
            get
            {
                return innerComment.IsApproved;
            }
            set
            {
                innerComment.IsApproved = value;
            }
        }

        public override string IP
        {
            get
            {
                return innerComment.IP;
            }
            set
            {
                if (innerComment != null)
                innerComment.IP = value;
            }
        }

        #endregion

        public string PermaLink
        {
            get
            {
                if (Article != null)
                    return new ArticleWrapper(Article).PermaLink + "#" + ID.ToString();
                return "";
            }
        }

        public bool IsAnonymousComment
        {
            get
            {
                if (string.IsNullOrEmpty(Email))
                    return true;

                if (!isAnonymous.HasValue)
                    isAnonymous = string.IsNullOrEmpty(Membership.GetUserNameByEmail(Email));

                return isAnonymous.Value;
            }
        }

        public string DisplayName
        {
            get
            {
                if (IsAnonymousComment)
                    return UserName;
                else
                    return Membership.GetUser(UserName).GetDisplayName();
            }
        }

        public string GetAvatar(HttpContextBase httpContext, int size = 80)
        {
            if (IsAnonymousComment)
                return "http://www.gravatar.com/avatar/" + MD5Hash(Email.ToLower()).ToLower() + (size > 0 ? "?s=" + size.ToString() : "");
            else
            {
                var user = Membership.GetUser(UserName);
                return user.GetAvatar(size);
            }
        }

        private bool? isAnonymous = null;

        private static string MD5Hash(string input)
        {
            // step 1, calculate MD5 hash from input
            MD5 md5 = MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hash = md5.ComputeHash(inputBytes);

            // step 2, convert byte array to hex string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }
    }
}
